<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--导入easyui样式 -->
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/themes/bootstrap/easyui.css" />
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/themes/icon.css" />
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/easyui-lang-zh_CN.js"></script>
<title>车辆管理</title>
</head>
<script type="text/javascript">
$(function () {
	$("#dg").datagrid({
		url:'selectCar',
		columns:[[   
		          {field:'ck',checkbox:true},
		          {field:'cid',title:'编号',width:100},    
		          {field:'plate',title:'车牌号',width:100},    
		          {field:'volume',title:'体积',width:100},    
		          {field:'weight',title:'重量',width:100},    
		          {field:'purchasing',title:'购买日期',width:100},    
		          {field:'state',title:'状态',width:100},    
		          {field:'driver',title:'司机',width:100},
		          {field:'position',title:'车辆位置',width:100},
		      ]],
		      rownumbers:true,
		      pagination:true,
		      pagePosition:'bottom',
		      pageSize:3,
		      pageList:[2,3,4],	
		      toolbar: [{
		  	iconCls: 'icon-add',
		  	text:'增加',
		  	handler: function(){
		  	$("#inserttea").dialog("open").dialog('setTitle', '增加车辆');		  	
		  	$('#cc').combobox({    
		  	url:'selectState',    
		  	valueField:'id',    
		  	textField:'text'   
		  	});
		  	$('#ss').combobox({    
			  	url:'selectPosition',    
			  	valueField:'id',    
			  	textField:'text'   
			  	}); 
		  	$('#cancl0').linkbutton({
					onClick:function(){
						$("#update").form('clear');  						
						$("#inserttea").dialog("close");
						 $('#dg').datagrid('load');
					}
				})								 
          $("input",$("#tb").next("span")).blur(function(){  
        		var plate =$("#tb").val();       		
		if(plate==""){
		    	$("#rsage2").show();
		}else{
			$.ajax({
				 url:'cxplate',
				 type:'post',
				 data:{'plate':plate},
				 datatype:'text',
				 success:function(data){
					 if(data=="1"){
						$("#rsage1").show();
					 }else{
						 $("rsage1").hide();
					 }
				 } 
			})
			$("#rsage2").hide();
			$("#rsage1").hide();
		}
	})  
		  	$("#save1").linkbutton({
		  	onClick:function(){		  		
		  	var plate =$("#tb").val();
		  	var volume =$("#tb1").val();
		  	var weight =$("#tb2").val();
		  	var driver =$("#tb3").val();
		  	var purchasing =$("#dd").datebox('getText');
		  	var state =$("#cc").combobox('getText');
		  	var position =$("#ss").combobox('getText');
		  	$.ajax({
		  	url:'addCars',
			type:'post',
			data:
		{'plate':plate,'volume':volume,'weight':weight,'driver':driver,'purchasing':purchasing,'state':state,'position':position},
		dataType:'text',
		success:function(xx){
			if(xx=="1"){
				$.messager.alert('信息','增加成功','info');
					$("#addcar").form('clear');  						
					$("#inserttea").dialog("close");
					 $('#dg').datagrid('load');
			}else{
				$.messager.alert('信息','增加失败','info');
			}
		}
		  	})}})			  			
		  		}
		  	},'-',{
		  		iconCls: 'icon-edit',
		  		text:'修改',
		  		handler: function(){
		  			
		  			var  stus= $('#dg').datagrid('getSelections');		  			
		  			if(stus.length==1){	  			
		  				$("#updatecar").dialog("open").dialog('setTitle', '修改车辆');
		  				var ss=stus[0].state;
		  				 if(ss=="运营"){
		  					$.messager.alert('信息','不能修改状态为运营的车辆信息','info');	
		  					$("#updatecar").dialog("close");
		  					 $('#dg').datagrid('load');
		  				 }else{
		  				$('#cc1').combobox({    
			  			  	url:'selectState',    
			  			  	valueField:'id',    
			  			  	textField:'text'   
			  			  	});
		  				$('#ss1').combobox({    
		  				  	url:'selectPosition',    
		  				  	valueField:'id',    
		  				  	textField:'text'   
		  				  	}); 
		  				$('#ck').val(stus[0].cid);
		  				$('#tbu').val(stus[0].plate);
		  				$('#tb1u').numberbox('setValue',stus[0].volume);		  		
		  				$('#tb2u').numberbox('setValue',stus[0].weight);		  				
		  				$('#tb3u').val(stus[0].driver);
		  				$('#dd1').datebox('setValue', stus[0].purchasing);	  			
		  				$('#cc1').combobox('setValue', stus[0].state);
		  				$('#ss1').combobox('setValue', stus[0].position);
		  				$('#cancl1').linkbutton({
		  					onClick:function(){
		  						$("#update").form('clear');  						
		  						$("#updatecar").dialog("close");
	  							 $('#dg').datagrid('load');
		  					}
		  				})
		  			
		  				$("#save2").linkbutton({
		  					onClick:function(){
		  						var cid=$("#ck").val();
		  						var plate =$("#tbu").val();
		  					  	var volume =$("#tb1u").numberbox('getValue');
		  					  	var weight =$("#tb2u").numberbox('getValue');  	
		  					  	var driver =$("#tb3u").val();
		  					  	var purchasing =$("#dd1").datebox('getText');
		  					  	var state =$("#cc1").combobox('getText');
		  					  var position =$("#ss1").combobox('getText');
		  					  	$.ajax({
		  					  	url:'updateCars',
		  						type:'post',
		  						data:
		  					{'plate':plate,'volume':volume,'weight':weight,'driver':driver,'purchasing':purchasing,'state':state,'position':position,'cid':cid},
		  					dataType:'text',
		  					success:function(yy){
		  						if(yy==1){
		  							$.messager.alert('信息','修改成功','info');
		  							$("#update").form('clear');  						
		  							$("#updatecar").dialog("close");
		  							 $('#dg').datagrid('load');
		  						} else{
		  							$.messager.alert('信息','修改失败','info');
		  						}
		  					}
		  					  	})
		  					}
		  				})
		  				 }
		  			}else {
		  				$.messager.alert('信息','请选择一条你要修改的信息','info');
		  				 $('#dg').datagrid('load');
		  			}
		  			
		  			
		  		}
		  	},'-',{
		  		iconCls: 'icon-remove',
		  		text:'删除',
				handler: function(){
					  
					var  stus= $('#dg').datagrid('getSelections');
					if(stus.length==0){
						$.messager.alert('error','请选择删除的内容！','info');
					}else{
						$("#delcar").dialog("open").dialog('setTitle', '删除车辆');
						for (var i = 0; i < stus.length; i++) {
	    	 				if(stus[i].state=="运营"){
	    	 				$.messager.alert('信息','所选信息不能包含状态为运营状态信息,请重新选择','info');
	    	 				$("#delcar").dialog("close");
	    	 				 $('#dg').datagrid('load');
	    	 					 break;
	    	 				}
	    	 				}
						$('#cancl2').linkbutton({
		  					onClick:function(){
		  						$("#update").form('clear');  						
		  						$("#delcar").dialog("close");
	  							 $('#dg').datagrid('load');
		  					}
		  				})
						$("#save3").linkbutton({
							onClick:function(){
								var cids =[];
								for (var i = 0; i < stus.length; i++) {
			    	 				cids.push(stus[i].cid);
			    	 				}
								$.ajax({
									url:'deleteCars',
			  						type:'post',
			  						data:
			  						{'cids':cids},
			  						 traditional:true,
			  						dataType:'text',
			  						success:function(zz){
			  							if(zz==1){
			  								$.messager.alert('信息','删除成功','info');			  							 						
				  							$("#delcar").dialog("close");
				  							 $('#dg').datagrid('load');
			  							}else{
			  								$.messager.alert('信息','删除失败','info');
			  							}
			  							
			  						}
								})
							}
						})

					}
				
				}
		  	}]
	
	});
	
})
</script>
<body>

<table id="dg"></table> 

<!--新增彈出窗口 -->
<div id="inserttea" class="easyui-dialog"  closed="true" buttons="#dlg2-buttons" style="width: 400px;height: 400px" >
<br>
<form id="addcar">
<div style="margin-top:10px;text-align: center;">
车牌号<input id="tb"name="plate" type="text" style="width:150px"class="easyui-textbox"data-options="required:true"/>
<span id="rsage1"  style="font-size:xx-small;display:none">车牌号已存在</span>
<span id="rsage2"  style="font-size:xx-small;display:none">不能用空</span><br><br>
体&nbsp;&nbsp;&nbsp;&nbsp;积<input id="tb1" name="volume"   class="easyui-numberbox"style="width:150px">立方<br><br>
重&nbsp;&nbsp;&nbsp;&nbsp;量<input id="tb2" name="weight" class="easyui-numberbox"style="width:150px">公斤<br><br>
司&nbsp;&nbsp;&nbsp;&nbsp;机<input id="tb3" name="driver" type="text" style="width:150px"><br><br>
车辆位置<input id="ss" name="position" value=""editable="false"><br><br>
购买日期<input id="dd" name="purchasing" type="text" class="easyui-datebox" required="required"editable="false"><br><br>
车辆状态<input id="cc"name="state"  value=""editable="false"><br><br>
</div>
</form>
<div id="dlg1-buttons" style="margin-top:10px;text-align: center;" >
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button id="save1"  class="easyui-linkbutton"iconcls="icon-save" >确定</button>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a id="cancl0" class="easyui-linkbutton"  ; iconcls="icon-cancel">取消</a>
</div>
</div>
<!--修改彈出窗口 -->
<div id="updatecar" class="easyui-dialog"  closed="true" buttons="#dlg2-buttons" style="width: 400px;height: 400px" >
<br>
<form id="update">
<div style="margin-top:10px;text-align: center;">
<input id="ck"name="cid" type="text"  style="display:none"><br><br>
车牌号<input id="tbu"name="plate" type="text" style="width:150px"><br><br>
体&nbsp;&nbsp;&nbsp;&nbsp;积<input id="tb1u" name="volume" class="easyui-numberbox" style="width:150px">立方<br><br>
重&nbsp;&nbsp;&nbsp;&nbsp;量<input id="tb2u" name="weight" class="easyui-numberbox" style="width:150px">公斤<br><br>
司&nbsp;&nbsp;&nbsp;&nbsp;机<input id="tb3u" name="driver" type="text" style="width:150px"><br><br>
车辆位置<input id="ss1" name="position" value=""editable="false"><br><br>
购买日期<input id="dd1" name="purchasing" type="text" class="easyui-datebox"editable="false"><br><br>
车辆状态<input id="cc1"name="state"  value=""editable="false"><br><br>
</div>
</form>
<div id="dlg1-buttons" style="margin-top:10px;text-align: center;" >
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button id="save2"  class="easyui-linkbutton"  iconcls="icon-save">确定</button>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a id="cancl1" class="easyui-linkbutton"  iconcls="icon-cancel">取消</a>
</div>
</div>
<!--删除彈出窗口 -->
<div id="delcar" class="easyui-dialog"  closed="true" buttons="#dlg3-buttons " style="width:250px;height: 250px" > 
<div id="dd2" style="margin-top:30px;text-align: center;" >
<h3>你确定要删除这些记录？</h3><br><br><br>
<input id="ck"name="cid" type="text"  style="display:none"><br><br>
<button id="save3"  class="easyui-linkbutton"  iconcls="icon-save">确定</button>
<a id="cancl2" class="easyui-linkbutton"  iconcls="icon-cancel">取消</a>
</div> 
</div>
</body>
</html>